package com.huy.mybatis;

import com.huy.bean.User;
import com.huy.mybatis.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MybatisMain {

    public static void main(String[] args) {
        String resource = "mybatis/mybatis-config.xml";
        Reader reader;
        SqlSession session=null;
        try {
            //将XML配置文件构建为Configuration配置类
            reader = Resources.getResourceAsReader(resource);
            // 通过加载配置文件流构建一个SqlSessionFactory  DefaultSqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            // 数据源 执行器 DefaultSqlSession
            session = sqlSessionFactory.openSession();
//            User user= session.selectOne("com.huy.mybatis.mapper.UserMapper.selectUserById", "bedff453e23411ef98f854e1adfb118e");
//            System.out.println(user.getUserName());

            UserMapper userMapper= session.getMapper(UserMapper.class);

//            List<User> list=userMapper.selectAll();
//            for (User user:list){
//                System.out.println("id:"+user.getId()+" crateTime:"+user.getCreateTime());
//            }

            User user= userMapper.selectUserById("58179417e23511ef98f854e1adfb118e");
            System.out.println("id:"+user.getId()+" crateTime:"+user.getCreateTime());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (session!=null){
                session.close();
            }
        }
    }
}
